import struct
from trace import Trace


class DecimalConversion:
    """
    进制转换工具
    """

    def __init__(self, decimal):
        pass

    @staticmethod
    def signed_int_to_binary(signed_int):
        """
        有符号整数转二进制，32位
        :param signed_int:
        :return:
        """
        # 使用struct打包为4字节，i表示有符号整数
        packed = struct.pack('>i', signed_int)
        print(f"struct packed 4 bytes: {packed}")
        # 将四字节数据转换位32位数据
        return ''.join(f'{c:08b}' for c in packed)

    @staticmethod
    def int_to_complement(signed_number: int, byte_count):
        """
        补码
        :return:
        """
        return signed_number.to_bytes(byte_count, 'little', signed=True)

    @staticmethod
    def signed_int_to_hex(signed_int):
        # 使用struct打包为4字节，i表示有符号整数
        # packed = struct.pack('>i', signed_int)
        packed = signed_int.to_bytes(2, 'little', signed=True)
        print(f"struct packed 4 bytes: {packed}")
        # 将四字节数据转换位32位数据
        return ''.join(f'{c:08b}' for c in packed)
